package com.uwlaser.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.modules.system.domain.SysLogs;
import com.uwlaser.modules.system.service.SysLogsService;
import com.uwlaser.modules.system.service.dto.SysLogsDto;
import com.uwlaser.modules.system.service.dto.SysLogsQueryParam;
import com.uwlaser.modules.system.service.mapper.SysLogsMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author liuzhihui
* @date 2022-03-27
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysLogsService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysLogsServiceImpl extends CommonServiceImpl<SysLogsMapper, SysLogs> implements SysLogsService {

    // private final RedisUtils redisUtils;
    private final SysLogsMapper sysLogsMapper;

    @Override
    public PageInfo<SysLogsDto> queryAll(SysLogsQueryParam query, Pageable pageable) {
        IPage<SysLogs> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysLogs> page = sysLogsMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, SysLogsDto.class);
    }

    @Override
    public List<SysLogsDto> queryAll(SysLogsQueryParam query){
        return ConvertUtil.convertList(sysLogsMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysLogsDto.class);
    }

    @Override
    public SysLogs getById(Integer id) {
        return sysLogsMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysLogsDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), SysLogsDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysLogsDto resources) {
        SysLogs entity = ConvertUtil.convert(resources, SysLogs.class);
        return sysLogsMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(SysLogsDto resources){
        SysLogs entity = ConvertUtil.convert(resources, SysLogs.class);
        int ret = sysLogsMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return sysLogsMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SysLogsDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysLogsDto sysLogs : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("请求类名", sysLogs.getClazzname());
              map.put("请求方法", sysLogs.getMethodname());
              map.put("请求参数", sysLogs.getParamter());
              map.put("返回参数", sysLogs.getReturns());
              map.put("耗时", sysLogs.getElapsedtime());
              map.put("请求时间", sysLogs.getCreateDate());
              map.put("类型", sysLogs.getType());
              map.put("异常类名", sysLogs.getErrorname());
              map.put("异常信息", sysLogs.getErrormsg());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/


    @Override
    public int clearLog(String saveDay) {
        return sysLogsMapper.clearLogs(saveDay);
    }
}
